/**
 * 
 */
package czolgisci.tiles;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.IOException;

/**
 * @author pillow
 * 
 */
public class Explosion extends ATile
{
	final int defaultLifeTime = 4;

	private int lifeTime = defaultLifeTime; // żywotność obiektu w tyknięciach
											// zegara

	public Explosion(String imagePath, Point pos) throws IOException
	{
		super(imagePath, pos);
	}

	public boolean hasExpired()
	{
		return (lifeTime <= 0);
	}

	public void decreaseLifeTime()
	{
		/* Create an ARGB BufferedImage */
		if (lifeTime != defaultLifeTime)
		{
			int w = image.getWidth(null);
			int h = image.getHeight(null);
			BufferedImage bi = new BufferedImage(w, h,
					BufferedImage.TYPE_INT_ARGB);
			Graphics g = bi.getGraphics();
			g.drawImage(image, 0, 0, null);

			/* Create a rescale filter op that makes the image 50% opaque */
			float[] scales =
			{ 1f, 1f, 1f, 0.5f };
			float[] offsets = new float[4];
			RescaleOp rop = new RescaleOp(scales, offsets, null);

			/* Draw the image, applying the filter */
			Graphics2D g2d = image.createGraphics();
			g2d.setBackground(new Color(0, 0, 0, 0));
			g2d.clearRect(0, 0, w, h);
			image.createGraphics().drawImage(bi, rop, 0, 0);

			g.dispose();
		}
		lifeTime--;
	}
}
